package com.lecture.java8.geekbang.demo04.ddd;

import com.google.protobuf.ServiceException;

import java.math.BigDecimal;

/**
 * Domain 领域模型（充血模型）
 * @author 72879
 */
public class VirtualWallet {

    private Long id;

    private Long createTime = System.currentTimeMillis();

    private BigDecimal balance = BigDecimal.ZERO;

    public VirtualWallet(Long preAllocatedId) {
        this.id = preAllocatedId;
    }

    public BigDecimal getBalance() {
        return this.balance;
    }

    public void debit(BigDecimal amount) throws ServiceException {
        if (this.balance.compareTo(amount) < 0) {
            throw new ServiceException("余额不足");
        }
        this.balance = balance.subtract(amount);
    }

    public void credit(BigDecimal amount) throws ServiceException {
        if (amount.compareTo(BigDecimal.ZERO) <0) {
            throw new ServiceException("金额不能是负数");
        }
        this.balance = balance.add(amount);
    }
}
